<?php
/**
 *  Copyright 2009 Michon van Dooren.
 *
 *  This file is part of DlScript.
 *
 *  DlScript is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  DlScript is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with DlScript.  If not, see <http://www.gnu.org/licenses/>.
 */
 
require_once('dlscript/functions.php');
require_once('dlscript/settings.php');
require_once('dlscript/template.php');

// Check the url to see if it is clean.
ini_set('url_rewriter.tags',        '');

// Load the template.
$template = new Template("dlscript/templates/{$dlscript_settings['template']}/");

$template->assign('sitename', $dlscript_settings['sitename']);

// Try to get the requested path.
$path = getreqpath();
if (!$path)
{
	error('Invalid path.', 404);
}

// Check if the requested path is a file.
if (is_file($path))
{
	// Get the base name of the download.
	$name = getname($path);
	
	// Checks if the file can be read.
	if (!is_readable($path))
	{
		error('File could not be read, please report this to the admin.', 500);
	}
	
	// Get the dir name.
	$dir = dirname($path);
	
	// Get the info of this download.
	$info = loadinfo("$dir/$name");
	
	// Check if this is a unique ip for this download, count it if it is.
	if (!isset($info['iplist'][$_SERVER['REMOTE_ADDR']]))
	{
		// Increase the main count.
		$info['count']++;
		
		// Increase the file specific count, creating the entries in the info first if needed.
		if (!isset($info['downloads'][getextention($path)]))
		{
			$info['downloads'][getextention($path)] = array('count' => 0);
		}		
		$info['downloads'][getextention($path)]['count']++;
		
		// Add the IP to the iplist.
		$info['iplist'][$_SERVER['REMOTE_ADDR']] = getextention($path);
	}
	
	// Save the updates info of this download.
	saveinfo("$dir/$name", $info);
	
	// Download the file.
	download($path);
}

else
{
	if (!$template->is_cached('filelist.tpl', "{$dlscript_settings['template']}|$path"))
	{
		// The array to contain all items to be displayed.
		$items = array(
			'dirs' => array(),
			'files' => array(),
		);
	
		// Scan the requested directory.
		$scan = scandir($path);
		foreach ($scan as $item)
		{
			// Check if it is a file, that is available for download.
			if (is_file("$path/$item") && getextention($item) && !is_hidden("$path/$item"))
			{
				$name = getname($item);
				$info = loadinfo("$path/$name");				
			
				// Add this file to the download info, if needed.
				if (!isset($info['downloads'][getextention($item)]))
				{
					$info['downloads'][getextention($item)] = array('count' => 0);
					saveinfo("$path/$name", $info);
				}	
				
				$items['files'][$name] = $info;
			}
		
			// Check if it is a folder.
			else if (is_dir("$path/$item") && !is_hidden("$path/$item"))
			{
				if ($item != '.' && $item != '..')
				{
					$items['dirs'][] = $item;
				}
			}
		}
	
		$template->assign('directories', 	$items['dirs']);
		$template->assign('downloads', 		$items['files']);
		$template->assign('is_root', 		($path == getcwd()));
		$template->assign('parent', 		"{$_GET['path']}/../../../");
	}
	
	$template->display('filelist.tpl', "{$dlscript_settings['template']}|$path");
}
